#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
比较base.yaml文件中同时包含fedora和rhel配置的包，检查它们的配置是否相同
"""
import yaml
import re
import os

def compare_fedora_rhel_configs(yaml_file_path):
    """
    分析yaml文件中fedora和rhel配置的情况，包括：
    - 同时包含fedora和rhel配置的包
    - fedora不存在但rhel存在的包
    - fedora存在但rhel不存在的包
    """
    try:
        with open(yaml_file_path, 'r', encoding='utf-8') as f:
            # 由于文件较大，使用yaml.safe_load读取
            data = yaml.safe_load(f)
        
        # 统计结果
        total_packages = len(data)
        packages_with_both = 0
        packages_with_different_configs = []
        packages_rhel_only = []  # 存储fedora不存在但rhel存在的包
        packages_fedora_only = []  # 存储fedora存在但rhel不存在的包
        
        # 遍历所有包
        for package_name, configs in data.items():
            if isinstance(configs, dict):
                # 检查fedora不存在但rhel存在的情况
                if 'fedora' not in configs and 'rhel' in configs:
                    packages_rhel_only.append({
                        'package': package_name,
                        'rhel': configs['rhel']
                    })
                # 检查fedora存在但rhel不存在的情况
                elif 'fedora' in configs and 'rhel' not in configs:
                    packages_fedora_only.append({
                        'package': package_name,
                        'fedora': configs['fedora']
                    })
                # 检查同时包含fedora和rhel的情况
                elif 'fedora' in configs and 'rhel' in configs:
                    packages_with_both += 1
                    
                    # 获取fedora和rhel的配置
                    fedora_config = configs['fedora']
                    rhel_config = configs['rhel']
                    
                    # 比较配置是否相同
                    if fedora_config != rhel_config:
                        packages_with_different_configs.append({
                            'package': package_name,
                            'fedora': fedora_config,
                            'rhel': rhel_config
                        })
        
        # 输出结果
        print(f"总包数量: {total_packages}")
        print(f"同时包含fedora和rhel配置的包数量: {packages_with_both}")
        print(f"fedora和rhel配置不同的包数量: {len(packages_with_different_configs)}")
        print(f"fedora不存在但rhel存在的包数量: {len(packages_rhel_only)}")
        print(f"fedora存在但rhel不存在的包数量: {len(packages_fedora_only)}")
        
        if packages_with_different_configs:
            print("\n配置不同的包详情:")
            for pkg_info in packages_with_different_configs:
                print(f"\n包名: {pkg_info['package']}")
                print(f"  fedora配置: {pkg_info['fedora']}")
                print(f"  rhel配置: {pkg_info['rhel']}")
        else:
            print("\n所有同时包含fedora和rhel配置的包，其配置都完全相同！")
        
        if packages_rhel_only:
            print("\nfedora不存在但rhel存在的包详情:")
            for pkg_info in packages_rhel_only:
                print(f"\n包名: {pkg_info['package']}")
                print(f"  rhel配置: {pkg_info['rhel']}")
        else:
            print("\n没有发现fedora不存在但rhel存在的包。")
        
        if packages_fedora_only:
            print("\nfedora存在但rhel不存在的包详情:")
            # for pkg_info in packages_fedora_only:
                # print(f"\n包名: {pkg_info['package']}")
                # print(f"  fedora配置: {pkg_info['fedora']}")
        else:
            print("\n没有发现fedora存在但rhel不存在的包。")
            
    except Exception as e:
        print(f"分析文件时出错: {e}")

if __name__ == "__main__":
    yaml_file = "d:/code/build-allinone/dep/base.yaml"
    if os.path.exists(yaml_file):
        compare_fedora_rhel_configs(yaml_file)
    else:
        print(f"文件不存在: {yaml_file}")